Title: Jpy Date: 2018-05-01 22:00 Category: Pelican Tags: pelican, python Slug: GradientDescent Author: KeitaW Summary: Test Jupyter notebook conversion

">

Title: Jpy Date: 2018-05-01 22:00 Category: Pelican Tags: pelican, python Slug: GradientDescent Author: KeitaW Summary: Test Jupyter notebook conversion

" />

Gradient Descent Algorithm

Title: Jpy Date: 2018-05-01 22:00 Category: Pelican Tags: pelican, python Slug: GradientDescent Author: KeitaW Summary: Test Jupyter notebook conversion

In [9]:
using Plots
using Distributions
gr(fmt="png");

Gradient Descent

Gradient descent, aka steepest descent algorithm is the first order optimization algorithm.

In [10]:
f(x, y) = 3x^2 + 5y^2 -6x*y
Out[10]:
f (generic function with 1 method)
In [11]:
function contourf(f, x=linspace(-50, 50, 50), y=linspace(-50, 50, 50))
    X = repmat(x', length(y), 1)
    Y = repmat(y, 1, length(x))
    Z = map(f, X, Y)
    contour(x, y, Z)
end
Out[11]:
contourf (generic function with 3 methods)
In [12]:
contourf(f)
Out[12]:
In [6]:
using Plots
gr(show = :ijulia) # in IJulia this would be: gr(show = :ijulia)
# x = 0:0.01:2*pi
# for i in 1:200 
#     display(plot(x, sin.(x + i / 10.0)))
# end
Out[6]:
Plots.GRBackend()
In [7]:
x = 0:0.01:2*pi
anim = @animate for i=1:100
    display(plot(x, sin.(x + i / 10.0)))
end
gif(anim, "/tmp/anim_fps15.gif", fps = 15);
gif(anim, "/tmp/anim_fps30.gif", fps = 30);
0 1 2 3 4 5 6 -0.75 -0.50 -0.25 0.00 0.25 0.50 0.75 y1
0 1 2 3 4 5 6 -0.75 -0.50 -0.25 0.00 0.25 0.50 0.75 y1
INFO: Saved animation to /tmp/anim_fps15.gif
INFO: Saved animation to /tmp/anim_fps30.gif

In [9]:
display("/tmp/anim_fps15.gif", read("/tmp/anim_fps15.gif"))
In [5]:
a = "~/Dropbox/00_tmp/8.png"
Out[5]:
"~/Dropbox/00_tmp/8.png"
In [ ]:
dir = "/tmp/anim_fps15.gif"
In [ ]:
using GR
inline("mov")
x = linspace(0, 2*pi, 100)
dt, nsteps = 0.03, 30
for n = 1:nsteps
    plot(x, sin(x - dt*n))
end
GR.show()
In [1]:
using GR
inline("mov")
x = linspace(0, 2*pi, 100)
dt, nsteps = 0.03, 30
for n = 1:nsteps
    plot(x, sin(x - dt*n))
end
GR.show()
WARNING: sin{T <: Number}(x::AbstractArray{T}) is deprecated, use sin.(x) instead.
Stacktrace:
 [1] depwarn(::String, ::Symbol) at ./deprecated.jl:70
 [2] sin(::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}) at ./deprecated.jl:57
 [3] macro expansion at ./In[1]:6 [inlined]
 [4] anonymous at ./<missing>:?
 [5] include_string(::String, ::String) at ./loading.jl:515
 [6] include_string(::Module, ::String, ::String) at /Users/kei/.julia/v0.6/Compat/src/Compat.jl:71
 [7] execute_request(::ZMQ.Socket, ::IJulia.Msg) at /Users/kei/.julia/v0.6/IJulia/src/execute_request.jl:158
 [8] (::Compat.#inner#17{Array{Any,1},IJulia.#execute_request,Tuple{ZMQ.Socket,IJulia.Msg}})() at /Users/kei/.julia/v0.6/Compat/src/Compat.jl:385
 [9] eventloop(::ZMQ.Socket) at /Users/kei/.julia/v0.6/IJulia/src/eventloop.jl:8
 [10] (::IJulia.##14#17)() at ./task.jl:335
while loading In[1], in expression starting on line 5
Out[1]:
In [1]:
using Plots; gr(); default(show=:inline);
for i=1:100
    histogram(randn(10000))
    sleep(0.2)
end
-4 -2 0 2 4 0 200 400 600 800 y1
In [2]:
for i=1:100
    histogram(randn(10000))
end
-4 -2 0 2 0 200 400 600 800 y1
In [1]:
using Plots
default(show=:ijulia)
bins = collect(-3:3); ylims = (0, 400)
p = histogram(randn(1000), bins = bins, ylims = ylims)

for i in 1:10
    h = histogram(randn(1000), bins = bins, ylims = ylims);
    p[1] = h.series_list[1][:x], h.series_list[1][:y]
    sleep(0.2)
    p
end
MethodError: no method matching setcharheight(::Float64)
The applicable method may be too new: running in world age 21831, while current world is 21832.
Closest candidates are:
  setcharheight(::Real) at /Users/kei/.julia/v0.6/GR/src/GR.jl:1071 (method too new to be called from this world context.)

Stacktrace:
 [1] #gr_set_font#463(::Symbol, ::Symbol, ::ColorTypes.RGB{FixedPointNumbers.Normed{UInt8,8}}, ::Int64, ::Function, ::Plots.Font) at /Users/kei/.julia/v0.6/Plots/src/backends/gr.jl:412
 [2] (::Plots.#kw##gr_set_font)(::Array{Any,1}, ::Plots.#gr_set_font, ::Plots.Font) at ./<missing>:0
 [3] gr_set_xticks_font(::Plots.Subplot{Plots.GRBackend}) at /Users/kei/.julia/v0.6/Plots/src/backends/gr.jl:605
 [4] _update_min_padding!(::Plots.Subplot{Plots.GRBackend}) at /Users/kei/.julia/v0.6/Plots/src/backends/gr.jl:654
 [5] _collect(::Array{RecipesBase.AbstractLayout,2}, ::Base.Generator{Array{RecipesBase.AbstractLayout,2},Plots.#_update_min_padding!}, ::Base.EltypeUnknown, ::Base.HasShape) at ./array.jl:454
 [6] _update_min_padding!(::Plots.GridLayout) at /Users/kei/.julia/v0.6/Plots/src/layouts.jl:310
 [7] prepare_output(::Plots.Plot{Plots.GRBackend}) at /Users/kei/.julia/v0.6/Plots/src/plot.jl:262
 [8] show(::Base.AbstractIOBuffer{Array{UInt8,1}}, ::MIME{Symbol("image/svg+xml")}, ::Plots.Plot{Plots.GRBackend}) at /Users/kei/.julia/v0.6/Plots/src/output.jl:209
 [9] #sprint#228(::Void, ::Function, ::Int64, ::Function, ::MIME{Symbol("image/svg+xml")}, ::Vararg{Any,N} where N) at ./strings/io.jl:66
 [10] display_dict(::Plots.Plot{Plots.GRBackend}) at /Users/kei/.julia/v0.6/Plots/src/output.jl:296
 [11] display(::IJulia.InlineDisplay, ::Plots.Plot{Plots.GRBackend}) at /Users/kei/.julia/v0.6/IJulia/src/inline.jl:77
 [12] inline(::Plots.Plot{Plots.GRBackend}) at /Users/kei/.julia/v0.6/Plots/src/output.jl:140
 [13] _do_plot_show(::Plots.Plot{Plots.GRBackend}, ::Symbol) at /Users/kei/.julia/v0.6/Plots/src/output.jl:155
 [14] _plot!(::Plots.Plot{Plots.GRBackend}, ::Dict{Symbol,Any}, ::Tuple{Array{Float64,1}}) at /Users/kei/.julia/v0.6/Plots/src/plot.jl:246
 [15] (::RecipesBase.#kw##plot)(::Array{Any,1}, ::RecipesBase.#plot, ::Array{Float64,1}) at ./<missing>:0
 [16] #histogram#668(::Array{Any,1}, ::Function, ::Array{Float64,1}, ::Vararg{Array{Float64,1},N} where N) at /Users/kei/.julia/v0.6/RecipesBase/src/RecipesBase.jl:381
 [17] (::Plots.#kw##histogram)(::Array{Any,1}, ::Plots.#histogram, ::Array{Float64,1}, ::Vararg{Array{Float64,1},N} where N) at ./<missing>:0
 [18] include_string(::String, ::String) at ./loading.jl:515
In [ ]:
using Plots
gr(fmt="png")
x = linspace(0, 2*pi, 100) |> collect 
dt, nsteps = 0.03, 100
for n = 1:nsteps
    IJulia.clear_output(true)
    Plots.plot(x, sin(x - n*dt))  |> display
end
In [13]:
l = @layout([[a;b] c])
p = plot(plot([sin,cos],1,leg=false),
         scatter([atan,cos],1,leg=false),
         plot(log,1,xlims=(1,10π),ylims=(0,5),leg=false),layout=l)
anim = Animation()
for x = linspace(1,10π,100)
    plot(push!(p,x,Float64[sin(x),cos(x),atan(x),cos(x),log(x)]))
    frame(anim)
end
In [55]:
plot(
    contourf(f),
    contourf(f),
    contourf(f),
    contourf(f)
)
Out[55]:
In [36]:
x = y = linspace(-50, 50, 50)
X = repmat(x', length(y), 1)
Y = repmat(y, 1, length(x))
f(x, y) = 3x^2 + 5y^2 -6x*y
Z = map(f, X, Y)
contour(x, y, Z)
Out[36]:
In [19]:
x = 1:0.5:20
y = 1:0.5:10
f(x,y) = begin
        (3x + y ^ 2) * abs.(sin.(x) + cos.(y))
    end
X = repmat(x',length(y),1)
Y = repmat(y,1,length(x))
Z = map(f,X,Y)
p1 = contour(x,y,f,fill=true)
p2 = contour(x,y,Z)
plot(p1,p2)
Out[19]:
In [11]:
xx=linspace(-1,1,100)
yy=xx
ff(x,y)=pdf(MvNormal([0.,0],[3. 2;2 3]),[x,y])
gg=[ff(x,y)::Float64 for x=xx,y=yy]
qlevels=[0,0.1,0.2,1]
levels=quantile(vec(gg),qlevels)

contour(xx,yy,gg,fill=true,levels=levels,c=:heat)    #figure 4
contour(xx,yy,gg,fill=true,levels=levels,c=ColorGradient(:heat,qlevels)) #figure 5
MethodError: Cannot `convert` an object of type Symbol to an object of type Array{ColorTypes.RGBA{Float64},1}
This may have arisen from a call to the constructor Array{ColorTypes.RGBA{Float64},1}(...),
since type constructors fall back to convert methods.

Stacktrace:
 [1] PlotUtils.ColorGradient(::Symbol, ::Array{Float64,1}) at /Users/kei/.julia/v0.6/PlotUtils/src/color_gradients.jl:107
 [2] include_string(::String, ::String) at ./loading.jl:515
In [15]:
ColorGradient(:heat, qlevels)
MethodError: Cannot `convert` an object of type Symbol to an object of type Array{ColorTypes.RGBA{Float64},1}
This may have arisen from a call to the constructor Array{ColorTypes.RGBA{Float64},1}(...),
since type constructors fall back to convert methods.

Stacktrace:
 [1] PlotUtils.ColorGradient(::Symbol, ::Array{Float64,1}) at /Users/kei/.julia/v0.6/PlotUtils/src/color_gradients.jl:107
 [2] include_string(::String, ::String) at ./loading.jl:515
In [21]:
?surface
search: surface surface! Surface schurfact schurfact!

Out[21]:

No documentation found.

Plots.surface is a Function.

# 1 method for generic function "surface":
surface(args...; kw...) in Plots at /Users/kei/.julia/v0.6/RecipesBase/src/RecipesBase.jl:381
In [19]:
f.(x, y)
Out[19]:
50-element Array{Float64,1}:
 200.0   
 184.007 
 168.68  
 154.019 
 140.025 
 126.697 
 114.036 
 102.041 
  90.7122
  80.05  
  70.0541
  60.7247
  52.0616
   ⋮     
  60.7247
  70.0541
  80.05  
  90.7122
 102.041 
 114.036 
 126.697 
 140.025 
 154.019 
 168.68  
 184.007 
 200.0   
In [ ]:
k
By @KeitaW in